import SQLite from '../common/SQLiteHelper'

let remote = ''

const path = require('path')
const fs = require('fs')

const userData = remote ? remote.app.getPath('userData') : ''
let configPath = path.resolve(userData, 'CKEDITOR/ckeditor_config.js')

if (!process.browser) {
  if (!fs.existsSync(path.dirname(configPath))) fs.mkdirSync(path.dirname(configPath))
  window.configPath = configPath
}

const fontNames = [
  // '黑体',
  // '宋体',
  // '仿宋',
  // '楷体',
  // '微软雅黑',
  // '隶书',
  '黑体',
  // '新宋体',
  // '仿宋',
  '楷体',
  '微軟正黑體',
  '微软雅黑',
  // 装Office会生出来的一些：
  '隶书',
  '幼圆',
  '华文细黑',
  '华文楷体',
  // '华文宋体',
  // '华文中宋',
  // '华文仿宋',
  '方正舒体',
  '方正姚体',
  '华文彩云',
  '华文琥珀',
  '华文隶书',
  '华文行楷',
  '华文新魏',
  'Arial',
  // 'Helvetica',
  'Tahoma',
  'Verdana',
  // 'Lucida Grande',
  'Times New Roman',
  'Georgia',
  // 'PmingLiu',
  'Impact',
  'Book Antiqua',
  'Century Gothic',
  'Courier New'
  // 'sans-serif',
]

/**
 * 初始化 CKEDITOR 配置文件
 * @returns {Promise<void>}
 */
export async function initCKEDITORConfig () {
  let fontSizeSizes = [
    { '初号': '42pt' },
    { '小初': '36pt' },
    { '一号': '26pt' },
    { '小一': '24pt' },
    { '二号': '22pt' },
    { '小二': '18pt' },
    { '三号': '16pt' },
    { '小三': '15pt' },
    { '四号': '14pt' },
    { '小四': '12pt' },
    { '五号': '10.5pt' },
    { '小五': '9pt' },
    { '六号': '7.5pt' },
    { '小六': '6.5pt' },
    { '七号': '5.5pt' },
    { '八号': '5pt' },
    { '20': '20pt' },
    { '28': '28pt' },
    { '48': '48pt' },
    { '72': '72pt' }
  ]

  let fontSizeSizesStr = ''
  for (let sizeObj of fontSizeSizes) {
    for (let name in sizeObj) {
      fontSizeSizesStr += `${name}/${sizeObj[name]};`
    }
  }

  let fontNamesStr = ''
  let res = await SQLite.GetSysFonts()
  res.forEach((item) => {
    if (fontNames.find((_item) => _item === item) || /宋/.test(item)) {
      fontNamesStr += `${item};`
    }
  })

  let str = `
            CKEDITOR.editorConfig = function (config) {
          
              config.extraPlugins ="mycustomplugin";
              config.removePlugins = 'elementspath';
              
              config.enterMode = CKEDITOR.ENTER_P; 
              config.shiftEnterMode = CKEDITOR.ENTER_P;
              config.allowedContent=true;
              // config.disallowedContent='pre';
              config.fullPage=false;
              // config.editingBlock = true;
              config.menu_groups ='clipboard,form,tablecell,tablecellproperties,tablerow,tablecolumn,table,anchor,link,/*image,*/flash,checkbox,radio,textfield,hiddenfield,imagebutton,button,select,textarea,mycustom';
            
              // config.height = 'auto';
              config.width = 'auto';
              config.skin = 'office2013';
              config.toolbarCanCollapse = false;//工具栏是否可以被收缩
              config.resize_enabled = false;
              config.colorButton_enableMore = true;
              config.tabSpaces = 4;
              config.forcePasteAsPlainText = false; //是否强制复制来的内容去除格式 plugins/pastetext/plugin.js
              config.disableObjectResizing = false;//是否开启 图片和表格 的改变大小的功能
              config.disableNativeTableHandles = false;//进行表格编辑功能 如：添加行或列 目前仅firefox支持
              config.pasteFromWordIgnoreFontFace = false;//当从word里复制文字进来时，是否进行文字的格式化去除
              config.pasteFromWordKeepsStructure = true;//是否使用h1、h2等标签修饰或者代替从word文档中粘贴过来的内容
              config.pasteFromWordRemoveStyle = false;//从word中粘贴内容时是否移除格式
              config.startupOutlineBlocks = false;//载入时，是否显示框体的边框
            
              config.filebrowserImageUploadUrl = "/back/news/imageUpload.do";
            
              // config.docType = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd%22';
            
            
              //字体编辑时的字符集 可以添加常用的中文字符：宋体、楷体、黑体等
              config.fontNames = '${fontNamesStr}';
            
              //字体编辑时可选的字体大小
              config.fontSizeSizes = '${fontSizeSizesStr}'
              config.line_height ="1; 1.5; 2; 3;";
            
              //字体默认大小显示文本
              config.fontSize_defaultLabel = '';
              //默认的字体名显示文本
              config.font_defaultLabel = '';
            
              config.font_style = {
                element: 'span',
                styles: {'font-family': '#(family)'},
                overrides: [{element: 'font', attributes: {'face': null}}]
              };
            
              config.toolbar = [
                {name:'mycustomplugin',items:['mySave','ExportToWord','-','Undo', 'Redo']},
               // {name: 'clipboard', items: ['Undo', 'Redo']},
                {name: 'styles', items: ['Font', 'FontSize', 'lineheight']},
                {
                  name: 'basicstyles',
                  items: ['Bold', 'Italic', 'Underline', 'Strike',/* 'CopyFormatting'*/]
                },
                {name: 'colors', items: ['TextColor', 'BGColor']},
                {
                  name: 'paragraph',
                  items: ['JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock', '-', 'NumberedList', 'BulletedList', '-','clearAllContent']
                },
                // {name: 'links', items: ['Link', 'Unlink']},
                // {name: 'insert', items: ['Table', 'base64image']},
              ];
            }; `
  fs.writeFileSync(configPath, str)

  console.log('initCKEDITORConfig:完成')
}
